package dog_mall_login.controller;

import dog_mall_commons.pojo.Dogs;
import dog_mall_commons.pojo.Users;
import dog_mall_login.service.DoLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class Login {
    @Autowired
    private DoLogin doLogin;

    @RequestMapping("/")
    public String index(){
        return "index";
    }

    @RequestMapping("/index")
    public String toindex(){
        return "index";
    }

    @RequestMapping("/login")
    public String Login(){
        return "login";
    }

    @RequestMapping("/register")
    public String register(){
        return "register";
    }

    @RequestMapping("/loginSuccess")
    public String loginSuccess(String username, String password, HttpServletRequest request){
        Users users = new Users();
        users.setUserUsername(username);
        users.setUserPassword(password);
        Users users1 = doLogin.DoLogin(users);
        // 登录成功
        if(users1!=null){
            request.getSession().setAttribute("user",users1);
            String dog_path = request.getSession().getServletContext().getRealPath("/dog_img/");
            request.getSession().setAttribute("dog_path","/dog_img/");
            request.getSession().setAttribute("user_path","/user_img/");
            // 把用户自己的狗信息提取放在session 用于显示
            List<Dogs> dogs = doLogin.findAllDogs(users1.getUserId());
            request.getSession().setAttribute("dog_img",dogs);
            request.getSession().setAttribute("user_img",users1.getUserImg());
            return "index";
        }
        // 登录失败
        else{
            return "login";
        }

    }
}
